<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Banner_model extends CI_Model
{
    public $bid;
    public $imgurl;
    public $isuse;
    public $weight;

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function Get_AllRow()
    {
        return $this->db->count_all_results('banner');
    }

    public function get_all_banner()
    {
        $sql="select * from banner where isuse=1 order by weight desc";
        return $this->db->query($sql)->result();
    }

    public function Get_BannerList_Page()
    {
        return $this->db->select()->from('banner')->where('bid>0')->order_by('weight', 'desc')->order_by('bid', 'asc')->get()->result();
    }

    public function Get_BannerList_Use_Limit($limit)
    {
        return $this->db->select()->from('banner')->where('isuse=1')->order_by('weight', 'desc')->order_by('bid', 'asc')->limit($limit)->get()->result();
    }

    public function Add_Banner($bannerModel)
    {
        return $this->db->insert('banner',$bannerModel);
    }

    public function Get_Banner_ById($bid)
    {
        return $this->db->select()->from('banner')->where('bid='.$bid)->get()->row();
    }

    public function Set_Banner_Weight($bannerModel)
    {
        return $this->db->query("update banner set weight=".$bannerModel->weight." where bid=".$bannerModel->bid);
    }

    public function Edit_Banner($bannerModel)
    {
        return $this->db->update('banner',$bannerModel,array('bid'=>$bannerModel->bid));
    }

    public function Del_Banner($bannerModel)
    {
        return $this->db->delete('banner',array('bid'=>$bannerModel->bid));
    }

    public function BatchDel_Banner($bidArray)
    {
        $this->db->trans_begin();
        foreach($bidArray as $biditem)
        {
            $this->db->delete('banner',array('bid'=>$biditem));
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }
}
?>
